import Vue from 'vue'
import VueRouter from 'vue-router'

import Login from '../views/loginPage.vue'
import IndexPage from '@/views/index/indexPage.vue'

// 导入其他小组件
import AdminManagement from '@/components/peopleManage/AdminManagement'
import UserManagement from '@/components/peopleManage/UserManagement'
import PersonalManagement from '@/components/peopleManage/PersonalManagement'
import LabelManage from '@/components/label/LabelManage'
import AdminInfo from '@/components/audit/AdminInfo'
import AvatarInfo from '@/components/audit/AvatarInfo'
import PostInfo from '@/components/audit/PostInfo'
import StoryInfo from '@/components/audit/StoryInfo'
import DeletePost from '@/components/content/DeletePost'
import DeleteStory from '@/components/content/DeleteStory'
import LoginLog from '@/components/log/LoginLog'
import OperationLog from '@/components/log/OperationLog'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/index',
    redirect: '/'
  },
  {
    path: '/',
    name: 'index',
    component: IndexPage,
    children: [
      // 主页的其他页面
      {
        path: '/admin-management',
        name: 'AdminManagement',
        component: AdminManagement // 管理员管理组件
      },
      {
        path: '/user-management',
        name: 'UserManagement',
        component: UserManagement // 用户管理组件
      },
      {
        path: '/personal-management',
        name: 'PersonalManagement',
        component: PersonalManagement // 个人信息管理组件
      },
      {
        path: '/label-manage',
        name: 'LabelManage',
        component: LabelManage // 标签管理组件
      },
      {
        path: '/admininfo-audit',
        name: 'AdminInfo',
        component: AdminInfo // 管理员信息审核组件
      },
      {
        path: '/avatarinfo-audit',
        name: 'AvatarInfo',
        component: AvatarInfo // 头像审核页面
      },
      {
        path: '/postinfo-audit',
        name: 'PostInfo',
        component: PostInfo // 帖子审核页面
      }, {
        path: '/storyinfo-audit',
        name: 'StoryInfo',
        component: StoryInfo // 故事审核页面
      },
      {
        path: '/delete-post',
        name: 'DeletePost',
        component: DeletePost // 删除帖子
      },
      {
        path: '/delete-story',
        name: 'DeleteStory',
        component: DeleteStory // 删除图片故事
      },
      {
        path: '/login-log',
        name: 'LoginLog',
        component: LoginLog // 登录日志
      },
      {
        path: '/operation-log',
        name: 'OperationLog',
        component: OperationLog // 操作日志
      }
    ]
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 如果目标路由是登录页面，则直接通过
  if (to.path === '/login') {
    next()
  } else {
    // 获取token
    const token = localStorage.getItem('access_token') || sessionStorage.getItem('access_token') || null
    console.log(token)

    // 如果token为空或undefined，且目标路由不是登录页面，则重定向到登录页面
    if (!token && to.path !== '/login') {
      next('/login')
    } else {
      // 如果token存在或者目标路由就是登录页面，则继续导航
      next()
    }
  }
})

export default router
